<template>
  <div class="home-container">
    <div class="hero-section">
      <h1 class="main-title">协同云图库 - Visual协同</h1>
      <p class="subtitle">
        一个创新的云图库平台，提供图片创作、管理、共享、协作功能，如艺术画、摄影作品，提升团队创意效率。
      </p>
      <div class="search-box">
        <el-input
          v-model="paramsData.searchQuery"
          placeholder="搜索你想要的图片"
          class="search-input"
          clearable
          @clear="handleSearch"
          @input="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" class="search-button">搜索</el-button>
      </div>
    </div>

    <div class="gallery-section">
      <div class="tabs-container">
        <div
          v-for="tab in tabs"
          :key="tab.value"
          :class="['tab-item', activeTab === tab.name ? 'active' : '']"
          @click="activeTabChange(tab)"
        >
          {{ tab.name }}
        </div>
      </div>

      <div class="gallery-grid">
        {{ pictures.length === 0 ? '暂无图片' : '' }}
        <div
          v-for="item in pictures"
          :key="item.id"
          class="gallery-item"
          @click="() => $router.push(`/picture/${item.id}`)"
        >
          <div class="image-container">
            <img :src="item.thumbnailUrl || item.url" :alt="item.name" class="gallery-image" />
          </div>
          <h3 class="item-title">{{ item.name }}</h3>
          <div class="item-author">作者: {{ item.user.userName }}</div>
          <div class="item-tags">
            <el-tag v-for="tag in item.tags" :key="tag" size="small" class="tag">{{ tag }}</el-tag>
          </div>
          <div class="item-stats">
            <div class="stat">
              <!-- 点赞数 -->
              <Icons name="icon-xihuan">
                {{ item.likeCount || 0 }}
              </Icons>
            </div>
            <div class="stat">
              <!-- 评论数 -->
              <el-icon><ChatRound /></el-icon>
              <span>{{ item.commentCount || 0 }}</span>
            </div>
            <div class="stat">
              <el-icon><Download /></el-icon>
              <span>{{ item.downloadCount || 0 }}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="paramsData.pageNum"
          :page-size="paramsData.pageSize"
          @current-change="cuurentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from 'vue'
import { getPictureListAPI } from '@/api/pictureAPI'
import { useClassifyStore } from '@/stores/classify'

const classifyStore = useClassifyStore()

const paramsData = reactive({
  pageNum: 1,
  pageSize: 8,
  searchQuery: '',
  classifyId: '',
})

const total = ref(0)

const activeTab = ref('全部')

const activeTabChange = (tab) => {
  activeTab.value = tab.name
  paramsData.classifyId = tab.name === '全部' ? '' : tab.value
  paramsData.pageNum = 1
  getPictureList()
}

const tabs = computed(() => {
  return [
    { name: '全部', value: 'all' },
    ...classifyStore.classifies.map((item) => ({
      name: item.name,
      value: item.id,
    })),
  ]
})

const pictures = ref([])

const getPictureList = async () => {
  console.log('paramsData', paramsData)

  const res = await getPictureListAPI(paramsData)
  if (res.code === 200) {
    console.log('获取图片列表成功', res.data)
    pictures.value = res.data.records
    total.value = res.data.total
    console.log(pictures.value)
    console.log('total.value', total.value)
  }
}
const handleSearch = () => {
  paramsData.pageNum = 1
  getPictureList()
}

const cuurentChange = (val) => {
  console.log('val', val)

  paramsData.pageNum = val
  getPictureList()
}

onMounted(() => {
  getPictureList()
})
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.hero-section {
  text-align: center;
  margin-bottom: 40px;
}

.main-title {
  font-size: 28px;
  color: #333;
  margin-bottom: 16px;
}

.subtitle {
  font-size: 16px;
  color: #666;
  max-width: 800px;
  margin: 0 auto 24px;
  line-height: 1.6;
}

.search-box {
  display: flex;
  max-width: 600px;
  margin: 0 auto;
}

.search-input {
  flex: 1;
}

.search-button {
  margin-left: 12px;
}

.gallery-section {
  margin-top: 40px;
}

.tabs-container {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 24px;
  overflow-x: auto;
  white-space: nowrap;
  padding-bottom: 4px;
}

.tab-item {
  padding: 8px 16px;
  cursor: pointer;
  color: #666;
  font-size: 14px;
  position: relative;
  transition: color 0.3s;
}

.tab-item:hover {
  color: #1677ff;
}

.tab-item.active {
  color: #1677ff;
  font-weight: 500;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1677ff;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
}

.gallery-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.gallery-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.image-container {
  height: 200px;
  overflow: hidden;
}

.gallery-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.gallery-item:hover .gallery-image {
  transform: scale(1.05);
}

.item-title {
  font-size: 16px;
  font-weight: 500;
  margin: 12px 12px 8px;
  color: #333;
  /* 单行溢出处理 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.item-author {
  font-size: 14px;
  color: #666;
  margin: 0 12px 8px;
  flex: 1;
}

.item-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 12px 12px;
}

.tag {
  font-size: 12px;
}

.item-stats {
  display: flex;
  justify-content: space-between;
  padding: 12px;
  background-color: #f9f9f9;
  border-top: 1px solid #eee;
}

.stat {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #666;
}

.pagination {
  margin-top: 40px;
  display: flex;
  justify-content: center;
}
</style>
